<template>
	<!-- 游戏内容页 -->
	<view class="evaluation">
		<view class="navbar" :style="navbarStyle">
			<navigator open-type="navigateBack">
				<!-- <uni-icons type="arrowleft" :size="30"></uni-icons> -->
				<image class="back" src="../../static/img/back.png"/>
			</navigator>
		</view>

		<eval-game :data="info" @playShowAd="showAd" v-if="info"></eval-game>
	</view>
</template>

<script>
	import EvalGame from '@/components/EvalGame/index.vue'
	let tuiwa = require("../../tuiwa.js")
	export default {
		components: {
			EvalGame
		},
		data() {
			return {
				barHeight: 0,
				info: null,
				videoAd: '',
				isShowAd:false,
				globalData:'',
				closeAd:''
			}
		},
		methods: {
			fetch() {
				const globalData = getApp().globalData
				this.barHeight = globalData.barHeight
				this.info = globalData.gameInfo
				console.log(this.info)
				uni.hideLoading()
			},
			showAd1( ) {
				this.onSkip()
			},
			showAd() {
				// this.onSkip()

				// hzzj
				let id = getApp().globalData.gameInfo.Id
				let remain_video_end = tt.getStorageSync('goods_id' + id)
				console.log('1111remain_video_end', remain_video_end, 'id', id);
				// if(remain_video_end) {
				// 	remain_video_end--
				// 	console.log(remain_video_end);
				// 	tt.setStorageSync('goods_id' + id, remain_video_end);
				// }
				if(remain_video_end == undefined || remain_video_end == '') {
					remain_video_end = 1
				}
				
				if (remain_video_end == 0) {
					console.log('是否进入')
					this.onSkip()
				} else {
					// zlzc
					if(this.isShowAd){
						return
					}
					this.isShowAd=true
					tuiwa.getAdEvent("videoStart")
					uni.showLoading({
						title: "加载中"
					})
					this.globalData.videoAd.show().then(() => {
						uni.hideLoading()
						this.isShowAd=false
						console.log("first show ad")
					}).catch(err => {
						this.globalData.videoAd.load().then(() => {
							this.globalData.videoAd.show().then(() => {
								this.isShowAd=false
								console.log("second show ad")
								uni.hideLoading()
							}).catch(err => {
								uni.hideLoading()
								this.isShowAd=false
								console.log("show ad fail",err)
								this.onSkip()
							})
						}).catch(err => {
							console.log("load ad fail",err)
							uni.hideLoading()
							this.isShowAd=false
							this.onSkip()
						});
					});
				}
			},
			onSkip() { 
				console.log(getApp().globalData.gameInfo.birthdayDate);
				let birthdayDate = getApp().globalData.gameInfo.birthdayDate
				uni.redirectTo({
					url: '/pages/result/result?template=' + this.info.template +
					 '&birthdayDate=' + birthdayDate
				})
			},
			onSuccess() {
				console.log("onSuccess=====")
				let id = getApp().globalData.gameInfo.Id
				let remain_video_end = tt.getStorageSync('goods_id' + id)
				if(remain_video_end){
					remain_video_end--
					console.log(remain_video_end);
					tt.setStorageSync('goods_id' + id, remain_video_end);
				}

				let birthdayDate = getApp().globalData.gameInfo.birthdayDate

				uni.redirectTo({
					url: '/pages/result/result?template=' + this.info.template +
					 '&birthdayDate=' + birthdayDate
				})
				// uni.redirectTo({
				// 	url: '/pages/result/result?template=' + this.info.template
				// })
				tuiwa.getAdEvent("videoEnd")
			},
		},
		computed: {
			navbarStyle() {
				return `height: ${40 + this.barHeight}px; padding-top: ${this.barHeight}px`
			}
		},
		onLoad(options) {
			console.log(options);
			if (options.xzId) {
				getApp().globalData.gameInfo.xzId = options.xzId
			}
			this.fetch()
			this.globalData = getApp().globalData
			this.globalData.videoAd = uni.createRewardedVideoAd({
				adUnitId: '14322h55hjdbc1852l'
			})
			if (this.globalData.videoAd) {
				this.globalData.videoAd.onError((err) => {
					console.log("ad err==",err)
				})
				var that=this
				this.closeAd=function closeHandler({ isEnded }) {
				  if (isEnded) {
				    that.onSuccess()
				  }
				}
				this.globalData.videoAd.onClose(this.closeAd)
			}
		},
		onUnload(){
			console.log("onUnload start")
			this.globalData.videoAd.offClose(this.closeAd)
		}

	}
</script>

<style lang="scss" scoped>
	.evaluation {
		position: relative;
		min-width: 100vw;
		min-height: 100vh;

		.navbar {
			position: fixed;
			width: 100%;
			display: flex;
			align-items: center;
			padding: 0 20rpx;
			z-index: 5;
		}

	}
	.back {
		width: 60rpx;
		height: 60rpx;
		margin-left: 20rpx;
	}
</style>
